<?php
header('Content-Type: text/html; charset=UTF-8');
if(!defined('INC_PATH')){
	define("INC_PATH","../../../");
}
require_once(INC_PATH."lib_new/db.php");
$day = date ("d",time());
$member_id = $_COOKIE['sav_memberid'];
$con = con_open();
$sql = " SELECT prize.prize,prize.no,prize.team,prize.num,win.point,win.changeTicket,prize.prize_date
FROM smd_takarakuji_prize as prize,smd_takarakuji_winproducts as win
WHERE date_trunc('month',prize.prize_date) = date_trunc('month',now() + '-1 months')
AND date_trunc('month',prize.prize_date) = date_trunc('month',win.use_date)
AND prize.prize = win.index
AND win.del_flag = 0
ORDER BY date_trunc('second',prize.prize_date) desc,prize limit 10 ;
";
$res = pg_query($con, $sql);
$sql_self = "
SELECT count(0) AS cnt FROM smd_takarakuji_get
WHERE member_id = ".$member_id."
AND prize > 0
AND date_trunc('month',regist_date) = date_trunc('month',now() + '-1 months')
";
$res_self = pg_query($con, $sql_self);
$cnt = pg_fetch_result($res_self, "cnt");
$self_value = $cnt > 0?1:0;

$sql_self_null = "
SELECT count(0) AS cnt FROM smd_takarakuji_get
WHERE member_id = ".$member_id."
AND prize = 0
AND flag = 0
AND date_trunc('month',regist_date) = date_trunc('month',now() + '-1 months')
";
$res_self_null = pg_query($con, $sql_self_null);
$null_count = pg_fetch_result($res_self_null, "cnt");
$self_value_null = $null_count>=10?1:0;

$sql_self_all = "
SELECT count(0) AS cnt FROM smd_takarakuji_get
WHERE member_id = ".$member_id."
AND (flag = 0 OR flag = 1)
AND date_trunc('month',regist_date) = date_trunc('month',now() + '-1 months')
";
$res_self_all = pg_query($con, $sql_self_all);
$all_count = pg_fetch_result($res_self_all, "cnt");

$prize01 = array();
$prize02 = array();
$prize03 = array();
$prize04 = array();
$prize05 = array();
$prize06 = array();
$prize07 = array();
$prize100 = array();
$prize101 = array();

$result = '<data>';
$result .= '<comment>当選結果</comment>';

if ($res){
	while ($row = pg_fetch_array($res)){
		$arr = array();
		$arr["no"] = "000000".$row["no"];
		$arr["team"] = $row["team"];
		$arr["num"] = $row["num"];
		$arr["point"] = $row["point"];
		$arr["changeticket"] = $row["changeticket"];
		if($row["prize"] == 1){
			$arr["no"] = substr($arr["no"],strlen($arr["no"]) - 6);
			$prize01[] = $arr;
		}else if($row["prize"] == 2){
			$arr["no"] = substr($arr["no"],strlen($arr["no"]) - 6);
			$prize02[] = $arr;
		}else if($row["prize"] == 3){
			$arr["no"] = substr($arr["no"],strlen($arr["no"]) - 5);
			$prize03[] = $arr;
		}else if($row["prize"] == 4){
			$arr["no"] = substr($arr["no"],strlen($arr["no"]) - 4);
			$prize04[] = $arr;
		}else if($row["prize"] == 5){
			$arr["no"] = substr($arr["no"],strlen($arr["no"]) - 3);
			$prize05[] = $arr;
		}else if($row["prize"] == 6){
			$arr["no"] = substr($arr["no"],strlen($arr["no"]) - 2);
			$prize06[] = $arr;
		}else if($row["prize"] == 7){
			$arr["no"] = substr($arr["no"],strlen($arr["no"]) - 1);
			$prize07[] = $arr;
		}else if($row["prize"] == 100){
			$arr["no"] = substr($arr["no"],strlen($arr["no"]) - 6);
			$prize100[] = $arr;
		}else if($row["prize"] == 101){
			$arr["no"] = substr($arr["no"],strlen($arr["no"]) - 6);
			$prize101[] = $arr;
		}
		
	}
	con_close($con);
}else{
	$result .= '<setVariable type="text" name="showSelf" value="'.$self_value.'" />';
	$result .= '<setVariable type="text" name="error" value="null" />';
	$result .= '<phpover />';
	$result .= '</data>';
	
	echo $result;
	con_close($con);
	exit;
}

//1等賞
$result .= '<setVariable type="text" name="showNum01" value="'.$prize01[0]["no"].'" />';
$result .= '<setVariable type="text" name="showTeam01" value="'.$prize01[0]["team"].'" />';
$result .= '<setVariable type="text" name="showCount01" value="'.$prize01[0]["num"].'" />';
$result .= '<setVariable type="text" name="showP01" value="'.$prize01[0]["point"].'" />';
$result .= '<setVariable type="text" name="showT01" value="'.$prize01[0]["changeticket"].'" />';

//2等賞
$result .= '<setVariable type="text" name="showNum02" value="'.$prize02[0]["no"].'" />';
$result .= '<setVariable type="text" name="showTeam02" value="'.$prize02[0]["team"].'" />';
$result .= '<setVariable type="text" name="showCount02" value="'.$prize02[0]["num"].'" />';
$result .= '<setVariable type="text" name="showP02" value="'.$prize02[0]["point"].'" />';
$result .= '<setVariable type="text" name="showT02" value="'.$prize02[0]["changeticket"].'" />';

//3等賞
$result .= '<setVariable type="text" name="showNum03" value="'.$prize03[0]["no"].'" />';
$result .= '<setVariable type="text" name="showTeam03" value="'.$prize03[0]["team"].'" />';
$result .= '<setVariable type="text" name="showCount03" value="'.$prize03[0]["num"].'" />';
$result .= '<setVariable type="text" name="showP03" value="'.$prize03[0]["point"].'" />';
$result .= '<setVariable type="text" name="showT03" value="'.$prize03[0]["changeticket"].'" />';

//4等賞
$result .= '<setVariable type="text" name="showNum04" value="'.$prize04[0]["no"].'" />';
$result .= '<setVariable type="text" name="showTeam04" value="'.$prize04[0]["team"].'" />';
$result .= '<setVariable type="text" name="showCount04" value="'.$prize04[0]["num"].'" />';
$result .= '<setVariable type="text" name="showP04" value="'.$prize04[0]["point"].'" />';
$result .= '<setVariable type="text" name="showT04" value="'.$prize04[0]["changeticket"].'" />';

//5等賞
$result .= '<setVariable type="text" name="showNum05" value="'.$prize05[0]["no"].'" />';
$result .= '<setVariable type="text" name="showTeam05" value="'.$prize05[0]["team"].'" />';
$result .= '<setVariable type="text" name="showCount05" value="'.$prize05[0]["num"].'" />';
$result .= '<setVariable type="text" name="showP05" value="'.$prize05[0]["point"].'" />';
$result .= '<setVariable type="text" name="showT05" value="'.$prize05[0]["changeticket"].'" />';

//6等賞
$result .= '<setVariable type="text" name="showNum06" value="'.$prize06[0]["no"].'" />';
$result .= '<setVariable type="text" name="showTeam06" value="'.$prize06[0]["team"].'" />';
$result .= '<setVariable type="text" name="showCount06" value="'.$prize06[0]["num"].'" />';
$result .= '<setVariable type="text" name="showP06" value="'.$prize06[0]["point"].'" />';
$result .= '<setVariable type="text" name="showT06" value="'.$prize06[0]["changeticket"].'" />';

//7等賞
$result .= '<setVariable type="text" name="showNum07" value="'.$prize07[0]["no"].'" />';
$result .= '<setVariable type="text" name="showTeam07" value="'.$prize07[0]["team"].'" />';
$result .= '<setVariable type="text" name="showCount07" value="'.$prize07[0]["num"].'" />';
$result .= '<setVariable type="text" name="showP07" value="'.$prize07[0]["point"].'" />';
$result .= '<setVariable type="text" name="showT07" value="'.$prize07[0]["changeticket"].'" />';

//前後賞
$result .= '<setVariable type="text" name="showNumleft" value="'.$prize100[0]["no"].'" />';
$result .= '<setVariable type="text" name="showTeamleft" value="'.$prize100[0]["team"].'" />';
$result .= '<setVariable type="text" name="showCountleft" value="'.$prize100[0]["num"].'" />';
$result .= '<setVariable type="text" name="showPleft" value="'.$prize100[0]["point"].'" />';
$result .= '<setVariable type="text" name="showTleft" value="'.$prize100[0]["changeticket"].'" />';
$result .= '<setVariable type="text" name="showNumright" value="'.$prize100[1]["no"].'" />';
$result .= '<setVariable type="text" name="showTeamright" value="'.$prize100[1]["team"].'" />';
$result .= '<setVariable type="text" name="showCountright" value="'.$prize100[1]["num"].'" />';
$result .= '<setVariable type="text" name="showPright" value="'.$prize100[1]["point"].'" />';
$result .= '<setVariable type="text" name="showTright" value="'.$prize100[1]["changeticket"].'" />';

//組違い賞
$result .= '<setVariable type="text" name="showNumteam" value="'.$prize101[0]["no"].'" />';
$result .= '<setVariable type="text" name="showTeamteam" value="'.$prize101[0]["team"].'" />';
$result .= '<setVariable type="text" name="showCountteam" value="'.$prize101[0]["num"].'" />';
$result .= '<setVariable type="text" name="showPteam" value="'.$prize101[0]["point"].'" />';
$result .= '<setVariable type="text" name="showTteam" value="'.$prize101[0]["changeticket"].'" />';


$result .= '<setVariable type="text" name="showSelf" value="'.$self_value.'" />';
$result .= '<setVariable type="text" name="can_janken" value="'.$self_value_null.'" />';
$result .= '<setVariable type="text" name="null_count" value="'.$null_count.'" />';
$result .= '<setVariable type="text" name="all_count" value="'.$all_count.'" />';
$result .= '<setVariable type="text" name="error" value="null" />';
$result .= '<phpover />';
$result .= '</data>';

echo $result;
?>